package com.tian.user.config;

import org.apache.commons.lang.StringUtils;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author zhumengping
 * @title: SwaggerConfig
 * @projectName zhump-cloud
 * @date 2023/8/8 14:53
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket getDocket(){
        Docket docket = new Docket(DocumentationType.OAS_30);
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        ApiInfo apiInfo = apiInfoBuilder.title("user 认证服务")
                .description("请遵守该文档规范")
                .version("v1.0.0.1")
                .contact(new Contact("F_M", "www.demo.org", "zhu_json@163.com"))
                .build();
        docket.apiInfo(apiInfo);
        return docket;
    }

    /**
    * <pre>
     *     swagger 3.0 增加 spring.pathmatch.matching-strategy=ant_path_matcher 不生效，需要加以下方法
     *     不然会报错
    * </pre>
    * @param webEndpointsSupplier
    * @param servletEndpointsSupplier
    * @param controllerEndpointsSupplier
    * @param endpointMediaTypes
    * @param corsEndpointProperties
    * @param webEndpointProperties
    * @param environment
    * @return org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping
    * @author zhump
    * @date 2023/8/9 9:20
    */
    @Bean
    public WebMvcEndpointHandlerMapping webMvcEndpointHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
                                                                     ServletEndpointsSupplier servletEndpointsSupplier,
                                                                     ControllerEndpointsSupplier controllerEndpointsSupplier,
                                                                     EndpointMediaTypes endpointMediaTypes,
                                                                     CorsEndpointProperties corsEndpointProperties,
                                                                     WebEndpointProperties webEndpointProperties,
                                                                     Environment environment){
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping =  webEndpointProperties.getDiscovery().isEnabled() &&
                (StringUtils.isBlank(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
        return new WebMvcEndpointHandlerMapping(endpointMapping,
                webEndpoints,
                endpointMediaTypes,
                corsEndpointProperties.toCorsConfiguration(),
                new EndpointLinksResolver(allEndpoints,basePath),
                shouldRegisterLinksMapping,null);

    }

}
